package com.feiclub.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.feiclub.subject.common.enums.CategoryTypeEnum;
import com.feiclub.subject.common.enums.IsDeletedFlagEnum;
import com.feiclub.subject.domain.convert.SubjectCategoryDomainConverter;
import com.feiclub.subject.domain.entity.SubjectCategoryBO;
import com.feiclub.subject.domain.service.SubjectCategoryDomainService;
import com.feiclub.subject.infra.basic.entity.SubjectCategory;
import com.feiclub.subject.infra.basic.service.SubjectCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    private  final SubjectCategoryService subjectCategoryService;
    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.bo:{}", JSON.toJSONString(subjectCategoryBO));
        }
//        把传过来的subjectCategoryBO 转换成subjectCategory
        SubjectCategory subjectCategory =
                SubjectCategoryDomainConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
//        逻辑删除采用默认值0 未传值 默认初始化值为0   也可以设置为0
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//    这是domain层    根据subjectCategory 调用infra的subjectCategoryService 操作数据库
        subjectCategoryService.insert(subjectCategory);
    }

    /*查询岗位大类*/
    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
//        把 categoryBO 变成  category
        SubjectCategory subjectCategory =
                SubjectCategoryDomainConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
//        查询的是逻辑删除字段IsDeleted为0的  表示未删除  也可以使用默认初始化值为0的值
    subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

//    大类的分类类型为1  小类的为2 因此查询大类还要把Category的CategoryType设置为0
//    但是查询大分类和小分类共用一个方法  一次接口传categoryType就可以不多写了
//        subjectCategory.setCategoryType(CategoryTypeEnum.PRIMARY.getCode());

//        根据 转换的category 调用subjectCategoryService层 查询数据库
        List<SubjectCategory> subjectCategoryList =
                subjectCategoryService.queryCategory(subjectCategory);

//        查询出的subjectCategoryList装换成subjectCategoryBOList
        List<SubjectCategoryBO> subjectCategoryBOList =
                SubjectCategoryDomainConverter.INSTANCE.converterBoListToCategoryList(subjectCategoryList);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.queryPrimaryCategory.boList:{}",
                    JSON.toJSONString(subjectCategoryBOList));
        }
//        返回subjectCategoryBOList
        return subjectCategoryBOList;
    }

    /**
     * 更新分类
     */
    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
//        把 categoryBO 变成  category
        SubjectCategory subjectCategory =
                SubjectCategoryDomainConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
//        调用infra层  与数据库交互
        int update = subjectCategoryService.update(subjectCategory);
        return update>0;

    }

    /**
     * 删除分类
     */
    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        //        把 categoryBO 变成  category
        SubjectCategory subjectCategory =
                SubjectCategoryDomainConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
        //        调用infra层  与数据库交互
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int update = subjectCategoryService.update(subjectCategory);
        return update>0;
    }


}
